package com.spzx.product.service.impl;

import com.alibaba.nacos.common.remote.client.Connection;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * 分类品牌Service业务层处理
 */
@Service
public class CategoryBrandServiceImpl
        extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
        implements ICategoryBrandService {
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;


    /**
     * 查询分类品牌列表
     *
     * @param categoryBrand 分类品牌
     * @return 分类品牌
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        Collections.reverse(categoryIdList);
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addCategoryBrand(CategoryBrand categoryBrand) {
        long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if (count > 0) {
            throw new ServiceException("该分类品牌已存在");
        } else {

            return categoryBrandMapper.insert(categoryBrand);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand databaseCategoryBrand = categoryBrandMapper.selectById(categoryBrand.getId());
        if (databaseCategoryBrand.getBrandId().longValue() == categoryBrand.getBrandId().longValue() &&
                databaseCategoryBrand.getCategoryId().longValue() == categoryBrand.getCategoryId().longValue()){
            throw new ServiceException("修改的分类品牌与原来的分类品牌相同");
        }
        if (databaseCategoryBrand.getBrandId().longValue() != categoryBrand.getBrandId().longValue() ||
            databaseCategoryBrand.getCategoryId().longValue() != categoryBrand.getCategoryId().longValue()){
            Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                    .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                    .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
            if (count > 0) {
                throw new ServiceException("该分类品牌已存在");
            }

            }
                 return categoryBrandMapper.updateById(categoryBrand);

    }

    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {

        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }
}

